package rad.framework.jsf;

import java.io.IOException;

import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.el.ValueBinding;

public class HtmlTable extends UIComponentBase {

	public static final String COMPONENT_FAMILY = "rad.framework.jsf.HtmlTable";

	public static final String COMPONENT_TYPE = "rad.framework.jsf.HtmlTable";

	private String width;

	// * CELLPADDING: space between the edge of a cell and the contents
	private String cellpadding;

	// * CELLSPACING: space between cells
	private String cellspacing;

	// TODO
	// * BORDER: size of border around the table
	// * BGCOLOR: color of the background
	// * BACKGROUND: picture to use as background
	// * ALIGN: alignment of table to surrounding text
	// * HSPACE: horizontal space between table and surrounding text
	// * VSPACE: vertical space between table and surrounding text
	// * HEIGHT: height of the table as a whole
	// * FRAME: parts of outside border that are visible
	// * RULES: if there should be internal borders
	// * BORDERCOLOR: color of border around the table
	// * BORDERCOLORLIGHT: color of "light" part of border around the table
	// * BORDERCOLORDARK: color of "dark" part of border around the table
	// * SUMMARY: Summary of t

	@Override
	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getCellpadding() {
		return cellpadding;
	}

	public void setCellpadding(String cellpadding) {
		this.cellpadding = cellpadding;
	}

	public String getCellspacing() {
		return cellspacing;
	}

	public void setCellspacing(String cellspacing) {
		this.cellspacing = cellspacing;
	}

	@Override
	public void restoreState(FacesContext context, Object state) {
		Object[] values = (Object[]) state;
		super.restoreState(context, values[0]);
		width = (String) values[1];
		cellpadding = (String) values[2];
		cellspacing = (String) values[3];
	}

	@Override
	public Object saveState(FacesContext context) {
		Object[] values = new Object[4];
		values[0] = super.saveState(context);
		values[1] = width;
		values[2] = cellpadding;
		values[3] = cellspacing;
		return values;
	}

	@Override
	public void encodeBegin(FacesContext context) throws IOException {
		if (!isRendered())
			return;

		super.encodeBegin(context);
		ResponseWriter response = context.getResponseWriter();
		response.startElement("table", this);

		response.writeAttribute("id", getClientId(context), "id");

		ValueBinding classBinding = getValueBinding("width");
		String width = classBinding == null ? this.width
				: (String) classBinding.getValue(context);
		if (width != null) {
			response.writeAttribute("width", width, "width");
		}

		classBinding = getValueBinding("cellpadding");
		String cellpadding = classBinding == null ? this.cellpadding
				: (String) classBinding.getValue(context);
		if (cellpadding != null) {
			response.writeAttribute("cellpadding", cellpadding, "cellpadding");
		}

		classBinding = getValueBinding("cellspacing");
		String cellspacing = classBinding == null ? this.cellspacing
				: (String) classBinding.getValue(context);
		if (cellspacing != null) {
			response.writeAttribute("cellspacing", cellspacing, "cellspacing");
		}
	}

	@Override
	public void encodeEnd(FacesContext context) throws IOException {
		if (!isRendered())
			return;

		ResponseWriter response = context.getResponseWriter();
		response.endElement("table");
		response.flush();
		super.encodeEnd(context);
	}

}
